<?php

namespace app\api\controller\paotui\poster;

use addons\paotui\library\PosterToken;
use app\admin\model\paotui\PosterExtractType;
use app\common\library\Sms;
use think\Validate;

/**
 * 骑手接口
 */
class Poster extends PosterApi
{
    protected $noNeedLogin = ['mobilelogin'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }

    //手机号登录
    public function mobilelogin(){

        $mobile = $this->request->post('mobile');
        $captcha = $this->request->post('captcha');
        if (!$mobile || !$captcha) {
            $this->error(__('Invalid parameters'));
        }
        if (!Validate::regex($mobile, "^1\d{10}$")) {
            $this->error(__('手机号不正确'));
        }

        if($captcha != "8888"){
            if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
                $this->error(__('验证码不正确'));
            }
        }

        $posterModel = new \app\admin\model\paotui\Poster();
        $posterInfo = $posterModel->where(['mobile'=>$mobile,'status'=>"1"])->find();
        if (empty($posterInfo)) {
            $this->error(__('当前手机号码还未注册骑手'));
        }
        if(empty($posterInfo['status'])){
            $this->error(__('当前手机号码已被锁定，请联系客服解锁'));
        }

        PosterToken::clear($posterInfo->id);
        $ret = $this->auth->direct($posterInfo->id);
        if ($ret) {
            $data = ['userinfo' => $this->auth->getUserinfo()];
            $this->success(__('Logged in successful'), $data);
        } else {
            $this->error($this->auth->getError());
        }

    }

    public function logout()
    {
        $this->auth->logout();
        $this->success(__('Logout successful'));
    }

    /**
     * 获取快递员信息
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function info(){
        $posterModel = new \app\admin\model\paotui\Poster();
        $posterInfo = $posterModel->where(['id'=>$this->auth->id,'status'=>1])->field("id,poster_name,is_order,mobile")->find();
        if($posterInfo){
            $posterService = new \addons\paotui\service\Poster();
            $posterInfo['todayCompleteNum'] = $posterService->getTodayCompletePostNum($this->auth->id);                  //获取今日完成订单
            $posterInfo['todayMoney'] = $posterService->getTodayMoney($this->auth->id);                                  //今日收益
        }

        $this->success('请求成功',$posterInfo);
    }


    /**
     * 完善骑手提现类型
     */
    public function extract_type(){
        $bank_name = $this->request->post("bank_name");
        $bank_code = $this->request->post("bank_code");
        $name = $this->request->post("name");
        $wechat = $this->request->post("wechat");
        $alipay = $this->request->post("alipay");
        $alipay_name = $this->request->post("alipay_name");

        $extractTypeModel = new PosterExtractType();
        if($extractTypeInfo = $extractTypeModel->where(['poster_id'=>$this->auth->id])->find()){
            if($extractTypeInfo->save([
                'bank_name'=>$bank_name,
                'bank_code'=>$bank_code,
                'name'=>$name,
                'wechat'=>$wechat,
                'alipay'=>$alipay,
                'alipay_name'=>$alipay_name,
                'updatetime'=>time()
            ])){
                $this->success("操作成功");
            }else{
                $this->error("操作失败");
            }
        }else{
            $extractTypeModel = new PosterExtractType();
            if($extractTypeModel->save([
                'poster_id'=>$this->auth->id,
                'bank_name'=>$bank_name,
                'bank_code'=>$bank_code,
                'name'=>$name,
                'wechat'=>$wechat,
                'alipay'=>$alipay,
                'alipay_name'=>$alipay_name,
                'updatetime'=>time()
            ])){
                $this->success("操作成功");
            }else{
                $this->error("操作失败");
            }
        }

    }

    public function extract_type_info(){
        $extractTypeModel = new PosterExtractType();
        $data = $extractTypeModel->where(['poster_id'=>$this->auth->id])->find();

        $this->success("请求成功",$data);
    }
}
